package com.ruoyi.project.naming.namesample.controller;

import com.alibaba.druid.util.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.naming.namesample.domain.NameSample;
import com.ruoyi.project.naming.namesample.service.INameSampleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 起名样例Controller
 *
 * @author shixiangwen
 * @date 2022-07-28
 */
@Controller
@RequestMapping("/naming/namesample")
public class NameSampleController extends BaseController {
    private String prefix = "naming/namesample";

    @Autowired
    private INameSampleService nameSampleService;

    @RequiresPermissions("naming:namesample:view")
    @GetMapping()
    public String namesample() {
        return prefix + "/namesample";
    }

    /**
     * 查询起名样例列表
     */
    @RequiresPermissions("naming:namesample:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(NameSample nameSample) {
        startPage();
        String loginName = ShiroUtils.getLoginName();
        if (StringUtils.equalsIgnoreCase(loginName, "namingts")) {
            nameSample.setBookId(2L);
        } else if (StringUtils.equalsIgnoreCase(loginName, "namingcc")) {
            nameSample.setBookId(3L);
        } else if (StringUtils.equalsIgnoreCase(loginName, "namingsj")) {
            nameSample.setBookId(4L);
        }
        List<NameSample> list = nameSampleService.selectNameSampleList(nameSample);
        return getDataTable(list);
    }

    /**
     * 导出起名样例列表
     */
    @RequiresPermissions("naming:namesample:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(NameSample nameSample) {
        List<NameSample> list = nameSampleService.selectNameSampleList(nameSample);
        ExcelUtil<NameSample> util = new ExcelUtil<NameSample>(NameSample.class);
        return util.exportExcel(list, "namesample");
    }

    /**
     * 新增起名样例
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存起名样例
     */
    @RequiresPermissions("naming:namesample:add")
    @Log(title = "起名样例", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(NameSample nameSample) {
        return toAjax(nameSampleService.insertNameSample(nameSample));
    }

    /**
     * 修改起名样例
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        NameSample nameSample = nameSampleService.selectNameSampleById(id);
        mmap.put("nameSample", nameSample);
        return prefix + "/edit";
    }

    /**
     * 修改保存起名样例
     */
    @RequiresPermissions("naming:namesample:edit")
    @Log(title = "起名样例", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(NameSample nameSample) {
        return toAjax(nameSampleService.updateNameSample(nameSample));
    }

    @RequiresPermissions("naming:namesample:edit")
    @Log(title = "更新性别", businessType = BusinessType.UPDATE)
    @PostMapping("/updateSex")
    @ResponseBody
    public AjaxResult updateSex(Long id, Integer sex) {
        NameSample nameSample = nameSampleService.selectNameSampleById(id);
        nameSample.setSex(sex);
        return toAjax(nameSampleService.updateNameSample(nameSample));
    }


    /**
     * 删除起名样例
     */
    @RequiresPermissions("naming:namesample:remove")
    @Log(title = "起名样例", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(nameSampleService.deleteNameSampleByIds(ids));
    }
}
